package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.ArticleComment;
import cn.wolfcode.trip.base.query.ArticleCommentQueryObject;
import cn.wolfcode.trip.base.service.IArticleCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/articleComments")
public class ArticleCommentController {
    @Autowired
    private IArticleCommentService articleCommentService;

    /*
        新增评论
        动作:POST
        资源:/articleComments
        参数:xx=xx&...
     */
    @PostMapping
    public Object save(ArticleComment entity, Long articleId, String type) {
        articleCommentService.saveComent(entity,articleId,type);
        Map<String, Object> map=new HashMap<>();
        map.put("articleCommentId",entity.getId());
        return map;
    }

    /*
        查询所有评论
        动作:GET
        资源:/comments/{type}
        参数:
     */
    @GetMapping
    public Object getComments(ArticleCommentQueryObject qo, Long articleId) {
        qo.setOrderBy("ac.releaseTime");
        return articleCommentService.query(qo,articleId);
    }

    /*
        查询某个评论
        动作:GET
        资源:/comments/{id}
        参数:
     */
    @DeleteMapping("/{id}/{type}")
    public Object getComment(@PathVariable Long id,@PathVariable String type) {
        JSONResult result = new JSONResult();
        try {
            articleCommentService.deleteComment(id,type);
        } catch (Exception e) {
            result.mark("操作失败,稍后再试");
        }
        return result;
    }
}

